package com.zhoufy.tomcat.utils;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created with IntelliJ IDEA by ChouFy on 2020-03-27.
 * <p>
 * 把请求信息封装成request对象
 *
 * @author zhoufy
 */
public class Request {

    /**
     * 请求方式  get/post
     */
    private String method;

    /**
     * 请求路径  index.html
     */
    private String url;

    /**
     * 输入流
     */
    private InputStream inputStream;


    public Request() {

    }

    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;
        int available = 0 ;
        while (available == 0){
            available = inputStream.available();
        }
        byte[] buffer = new byte[available];
        inputStream.read(buffer);
        String inputText = new String(buffer);
        //获取请求头第一行信息  GET / HTTP/1.1
        String firstLine = inputText.split("\\n")[0];

        String[] split = firstLine.split(" ");
        this.method = split[0];
        this.url = split[1];
        System.out.println(method + " " + url);
    }


    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }
}
